package cn.fl.product.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.product.service.IPrdCarInfoService;
import cn.fl.product.transform.PrdCarInfoVOTransform;
import cn.fl.product.vo.PrdCarInfoVO;
import fl.product.facade.product.api.IProductApiService;
import fl.product.facade.product.dto.PrdCarInfoDTO;

/**
 * @Description: 乘用车/专车产品信息表 客户端服务层接口实现
 * @Description: 持久层方法名称前缀：新增：insert* 删除：delete* 修改：update* 批量：batch* 查询对象：select* get* find* 查询集合：select*
 */
@Service
public class PrdCarInfoServiceImpl implements IPrdCarInfoService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(PrdCarInfoServiceImpl.class);
	
	/**
	 * 产品 API 暴露服务
	 */
	@Autowired
	private IProductApiService productApiService;

	@Override
	public PrdCarInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "根据主键查询失败,主键参数为空.ID:"+id,CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		PrdCarInfoDTO dto = this.productApiService.selectPrdCarInfoById(id, CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		PrdCarInfoVO vo = PrdCarInfoVOTransform.toVO(dto);
		if(null!=vo && null!=vo.getGuidePc()){
			vo.setGuidePcDouble_tkBus(DecimalUtils.convertFenToYuan(vo.getGuidePc()));
		}
		return vo;
	}

	@Override
	public Long add(PrdCarInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增失败,对象参数为空.DATA:"+com.alibaba.fastjson.JSONObject.toJSONString(entity),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.productApiService.addPrdCarInfo(PrdCarInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return false;
		//return this.prdCarInfoApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.info(logger, "removeByIds", "删除乘用车(专车)失败,主键参数为空.IDS:"+com.alibaba.fastjson.JSONObject.toJSONString(ids),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.productApiService.removePrdCarInfoByIds(ids);
    }

	@Override
	public boolean modify(PrdCarInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改失败,对象参数为空.DATA:"+com.alibaba.fastjson.JSONObject.toJSONString(entity),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		 PrdCarInfoDTO dto = PrdCarInfoVOTransform.toDTO(entity);
		return this.productApiService.modifyPrdCarInfo(dto);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.productApiService.searchPrdCarInfoListPage(page, CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		List<PrdCarInfoDTO> resultList = (List<PrdCarInfoDTO>) page.getResultList();
		List<PrdCarInfoVO> voList = PrdCarInfoVOTransform.toVOList(resultList);
		page.setResultList(voList);
		return page;
	}

	@Override
	public List<PrdCarInfoVO> getPrdCarInfoByPrdId(Long id) {
		return  PrdCarInfoVOTransform.toVOList(productApiService.getPrdCarInfoByPrdId(id));
	}

	@Override
	public Pager getPrdCarInfoPage(Pager page,String sysCd,String orgCd) throws BaseException {
		return productApiService.getPrdCarInfoPage(page,sysCd,orgCd);
	}

	@Override
	public PrdCarInfoVO selectPrdCarById(Long id) throws BaseException {
		PrdCarInfoDTO dto = this.productApiService.selectPrdCarById(id, CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		return PrdCarInfoVOTransform.toVO(dto);
	}
	
	@Override
	public boolean modifyAudStsCd(Long id, String audStsCd, Date mdfTm,
			Long mdfUsrId) throws BaseException {
		if(StringUtils.isEmpty(id)||StringUtils.isEmpty(audStsCd)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.productApiService.modifyPrdTkBusInfoAudStsCd(id, audStsCd, mdfTm, mdfUsrId);
	}

}
